package com.example.day14.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.day14.entity.Student;
import com.example.day14.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Linyh
 * @Description
 * @Date 2022/7/22 9:36
 **/

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //1）查询所有学生信息
    @RequestMapping("/listAll")
    public List<Student> listAll() {
        return studentService.list();
    }

    //2）根据ID查询学生信息
    @RequestMapping("/findId")
    public Student findId(@RequestParam Long id) {
        return studentService.getById(id);
    }

    //3）模糊查询学生信息
    @RequestMapping("/likeName")
    public List<Student> likeName(@RequestParam String sname) {
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like("sname", sname);
        List<Student> list = studentService.list(wrapper);
        return list;
    }

    //4）分页查询学生信息
    @RequestMapping("/pageList")
    public Long pageList() {
        Page<Student> page = new Page<>(1, 6);
        studentService.page(page);
        page.getRecords();
        System.out.println(page.getTotal());
        return page.getTotal();
    }

    //5）添加学生信息
    @RequestMapping("/add")
    public boolean add(@RequestBody Student student) {
        return studentService.save(student);
    }

    //6）修改学生信息
    @RequestMapping("/update")
    public boolean update(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    //7）根据ID删除学生
    @RequestMapping("/deleteId")
    public boolean deleteId(@RequestParam long sid) {
        return studentService.removeById(sid);
    }

    //8）批量删除学生
    @RequestMapping("deleteList")
    public boolean deleteList(@RequestBody List<Long> ids) {
        return studentService.removeByIds(ids);
    }
}
